package ExtractPostContents;

use Carp;
use Mojo::DOM;

use Exporter qw( import );
our @EXPORT = qw( extract_post_details) ;

sub extract_post_details {
	my ($url, $contents) = @_;

	my $dom = Mojo::DOM->new($$contents);
	
	binmode(STDOUT, ':utf8');

	my $post_title = $dom->find('div.entry > h1.post-title.entry-title')->all_text();
	
	my $post_categories = $dom->find('div.entry > div.post-meta > p.meta_categories > a')->all_text();
	my @post_categories;
	if ($post_categories) {
		@post_categories = split(/\n/, $post_categories);
	}

	my $post_tags = $dom->find('div.entry-footer > p.post-tags > a')->all_text();
	my @post_tags = ('');
	if ($post_tags) {
		@post_tags = split(/\n/, $post_tags);
	}

	# Apparently, the HTML differs for some of the posts. As an example, http://www.jeyamohan.in/?p=20684
	# Here, there is a <span> tag after the div class entry-content, whereas it is the <p> tag
	# in most other cases. 
	#
	# This not surprising, and this is what I get for using parsing techniques specific to a markup / design.
	# I don't have enough brains to roll my own HTML parser. 
	#
	# I did try readability API. However, it doesn't always parse the contents properly. I have seen many posts 
	# where either, it repeatedly appended multiple times, or it seems to go after the links in 
	# the "Related links" section and seems to extract them as well.
	# Enough of rant. As a horrible workaround, I have included another "if", that has bit of a greedy find at the
	# top-level div. This brings the entire post including the related sections links.
	#
	# Not sure if how many more edge cases I have missed. 
	
	my @tags_to_be_removed = ('[href=https://twitter.com/share]',
								'[href=http://www.facebook.com/sharer.php]',
								'hr',
								'g\:plusone',
								'[href="javascript:print();"]'
								);

	foreach my $tag (@tags_to_be_removed) {
		print $url, ": Removing ", $tag, " for ", $url, "\n";
	 	my $tag_to_remove = $dom->at($tag);

	 	if($tag_to_remove) {
	 		$page_contents = $dom->at($tag)->remove->root;
	 	}
	 	else {
	 		print $url, ": Could not find ", $tag, "\n";
	 	}
	 }

	my $post_contents = $dom->find('div.entry-content');
	if (! $post_contents) {
		confess $url.": Unable to parse contents. Check the HTML markup.";
	}

	my $post_contents_text = '';
	$post_contents_text = $dom->find('div.entry > div.entry-content')->all_text();

	# Additionally, there are posts that are in Q&A format. The count here will include
	# words in the questions as well. 
	#
	# The point is, the count may not reflect the amount of words written by the author accurately.

	my $all_word_count = split(/ /, $post_contents_text);
	my $related_posts_text = $dom->find('div.entry > div.entry-content > ul.related_post')->all_text();

	$all_word_count -= (scalar(split(/ /, $related_posts_text)) - 2); # 2 - excluding the two words "Related Links"
	if ($all_word_count <= 0) {
		warn $url." : word count negative. Check this URL\n";
	}


	($post_title, \@post_categories, \@post_tags, \$post_contents, $all_word_count);
}